<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="门店编码" prop="storageCode">
        <el-input
          v-model="queryParams.storageCode"
          placeholder="请输入门店编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="storageName">
        <el-input
          v-model="queryParams.storageName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="门店类型" prop="storageType">
        <el-input
          v-model="queryParams.storageType"
          placeholder="请输入门店类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactMobileNo">
        <el-input
          v-model="queryParams.contactMobileNo"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="openForm('create')" v-if="isAdministrator()">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="index" width="50" />
      <!-- <el-table-column label="ID" align="center" prop="storageId" width="80" /> -->
      <el-table-column label="门店编码" align="center" prop="storageCode" width="120" />
      <el-table-column label="门店名称" align="center" prop="storageName" width="150" />
      <el-table-column label="门店地址" align="center" prop="storageAddress" width="200" />
      <el-table-column label="门店类型" align="center" prop="storageType" width="100" />
      <el-table-column label="门店金额" align="center" prop="storageMoney" width="100">
        <template #default="scope">
          {{ scope.row.storageMoney ? `¥${scope.row.storageMoney}` : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="门店星级" align="center" prop="storageStar" width="130">
        <template #default="scope">
          <el-rate v-model="scope.row.storageStar" disabled show-score />
        </template>
      </el-table-column>
      <el-table-column label="管家星级" align="center" prop="housekeeperStar" width="130">
        <template #default="scope">
          <el-rate v-model="scope.row.housekeeperStar" disabled show-score />
        </template>
      </el-table-column>
      <el-table-column label="客服热线" align="center" prop="serviceHotline" width="120" />
      <el-table-column label="所在省" align="center" prop="provinceName" width="100" />
      <el-table-column label="所在市" align="center" prop="cityName" width="100" />
      <el-table-column label="所在区" align="center" prop="regionName" width="100" />
      <el-table-column label="经度" align="center" prop="longitude" width="80" />
      <el-table-column label="纬度" align="center" prop="latitude" width="80" />
      <el-table-column label="促销折扣" align="center" prop="promotionalDiscount" width="100" />
      <el-table-column label="促销开始时间" align="center" prop="promotionalStartTime" width="150">
        <template #default="scope">
          {{ scope.row.promotionalStartTime ? formatDate(scope.row.promotionalStartTime) : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="促销结束时间" align="center" prop="promotionalEndTime" width="150">
        <template #default="scope">
          {{ scope.row.promotionalEndTime ? formatDate(scope.row.promotionalEndTime) : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="促销详情" align="center" prop="promotionalDetails" width="150" />
      <el-table-column label="联系人名称" align="center" prop="contactName" width="100" />
      <el-table-column label="联系电话" align="center" prop="contactMobileNo" width="120" />
      <el-table-column label="状态" align="center" prop="enable" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.enable === 1" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="100" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="修改人" align="center" prop="updateBy" width="100" />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="remark" width="150" />
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.storageId)">
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.storageId)"
            v-if="isAdministrator()"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <BaseStorageForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { BaseStorageApi, BaseStorageVO } from '@/api/crm/basestorage'
import BaseStorageForm from './BaseStorageForm.vue'

import { isAdministrator } from '@/hooks/web/useCache'
/** 门店 列表 */
defineOptions({ name: 'BaseStorage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BaseStorageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  storageCode: undefined,
  storageName: undefined,
  storageType: undefined,
  contactName: undefined,
  contactMobileNo: undefined,
  enable: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseStorageApi.getBaseStoragePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BaseStorageApi.deleteBaseStorage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BaseStorageApi.exportBaseStorage(queryParams)
    download.excel(data, '门店.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
